
import React, { Suspense , lazy} from 'react'

const Layout = lazy(() => import('@/Layout'))
const Login = lazy(() => import('@/pages/Login/Login'))
const NotFound = lazy(() => import('@/pages/NotFound'))
// 二级路由
const Home = lazy(() => import('@/pages/Home'))
const Profile = lazy(() => import('@/pages/Profile'))
const Question = lazy(() => import('@/pages/Question'))
const Video = lazy(() => import('@/pages/Video'))
const Article = lazy(() => import('@/pages/Article'))

const lazyLoad = (children) => {
  return <Suspense fallback={<>loading</>}>{children}</Suspense>
}
const router = [
  {
    path: '/',
    element: <Layout/>,
    auth: true,
    children: [
      {
        index: true,
        
        element: lazyLoad(<Home/>),
        auth: false,
      },
      {
        path: '/profile',
        
        element: lazyLoad(<Profile/>),
        auth: true,
      },
      {
        path: '/question',
        
        element: lazyLoad(<Question/>),
        auth: false,
      },
      {
        path: '/video',
        
        element: lazyLoad(<Video />),
        auth: false,
      }
    ],
},
{
  path: '/login',
  element: lazyLoad(<Login />),
  auth: false,
},
{
  path: '/article/:id',
  element: lazyLoad(<Article />),
  auth: false,
},
{
  path: '*',
  element: lazyLoad(<NotFound />),
  auth: false,
},
]

// 抓取当前路由
const checkAuth = (router,path) => {
  for (const data of router) {
    if( data.path === path ) {
      return data
    }
    if (data.children) {
      const res = checkAuth( data.children , path )
      if (res) {
        return res
      }
    }
  }
  return null
}

const checkRouterAuth = (path) => {
  let authObj = null
  authObj = checkAuth( router , path )
  return authObj
}

export { router , checkRouterAuth }